//
//  ChartViewController.m
//  Pad-test
//
//  Created by jjkj on 17/3/1.
//  Copyright © 2017年 jjkj. All rights reserved.
//

#import "ChartViewController.h"
//折线图头文件
#import "DVLineChartView.h"
#import "UIView+Extension.h"
#import "UIColor+Hex.h"
//柱形图头文件
#import "DVBarChartView.h"
//饼形图头文件
#import "CustomPieView.h"
#define RGBCOLOR(r,g,b,_alpha) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:_alpha]
#define PhoneScreen_HEIGHT [UIScreen mainScreen].bounds.size.height
#define PhoneScreen_WIDTH [UIScreen mainScreen].bounds.size.width
@interface ChartViewController ()<DVBarChartViewDelegate,DVLineChartViewDelegate>
{
    CustomPieView *PieChartView;
    
    NSMutableArray *segmentDataArray;
    
    NSMutableArray *segmentTitleArray;
    
    NSMutableArray *segmentColorArray;
    
    NSInteger chartWidth;
    
    NSInteger chartHeight;
}

@end

@implementation ChartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.frame = CGRectMake(190, 149, PhoneScreen_WIDTH-190, PhoneScreen_HEIGHT-149);
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self LineChart];
    
    [self BarChart];

    [self loadPieData];
    
    [self loadPieChartView];
}

//-----------折线图--------------
- (void)LineChart{
    //self.view.backgroundColor = [UIColor colorWithHexString:@"3e4a59"];
    
    DVLineChartView *ccc = [[DVLineChartView alloc] init];
    
    ccc.frame = CGRectMake(0, 100, self.view.width, 450);
    [self.view addSubview:ccc];
    
    //ccc.width = self.view.width;
    
    //y轴与左侧的间距
    ccc.yAxisViewWidth = 52;
    //y轴的最大值
    ccc.yAxisMaxValue = 300;
    //y轴分为几段(默认5)
    ccc.numberOfYAxisElements = 5;
    
    ccc.delegate = self;
    //点是否允许点击
    ccc.pointUserInteractionEnabled = YES;
    //点与点之间的间距
    ccc.pointGap = 50;
    //是否显示横向分割线
    ccc.showSeparate = YES;
    //横向分割线的颜色
    ccc.separateColor = [UIColor colorWithHexString:@"67707c"];
    ccc.textColor = [UIColor colorWithHexString:@"9aafc1"];
    //ccc.textColor = [UIColor blackColor];
    ccc.backColor = [UIColor colorWithHexString:@"3e4a59"];
    //ccc.backColor = [UIColor whiteColor];
    //坐标轴颜色
    ccc.axisColor = [UIColor colorWithHexString:@"67707c"];
    //x轴的文字集合
    ccc.xAxisTitleArray = @[@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30"];
    
    //CGRectMake(x,y,width,height)
    ccc.x = 0;
    ccc.y = 50;
    //ccc.width = self.view.width;
    //ccc.height = self.view.height - 200;
    
    //点的位置集合
    DVPlot *plot = [[DVPlot alloc] init];
    plot.pointArray = @[@10, @50, @170, @100, @150, @190, @160, @130, @210, @70, @200, @150, @100, @200, @170, @190, @260, @110, @210, @80, @100, @150, @300, @200, @170, @180, @160, @130, @150, @210];
    
    plot.lineColor = [UIColor colorWithHexString:@"2f7184"];
    plot.pointColor = [UIColor colorWithHexString:@"14b9d6"];
    //是否将颜色填满图表
    plot.chartViewFill = YES;
    //带点 or 不带点
    plot.withPoint = YES;
    
    [ccc addPlot:plot];
    [ccc draw];
}

//-------------柱形图--------------
- (void)BarChart{
    
    DVBarChartView *chartView = [[DVBarChartView alloc] initWithFrame:CGRectMake(0, 525, self.view.frame.size.width/2 - 50, 325/*self.view.frame.size.height - 200*/)];
    [self.view addSubview:chartView];
    
    //y轴与左侧的间距
    chartView.yAxisViewWidth = 52;
    //y轴的最大值
    chartView.yAxisMaxValue = 15;
    //y轴分为几段
    chartView.numberOfYAxisElements = 3;
    //x轴所有点的集合
    chartView.xAxisTitleArray = @[@"1号牙轮", @"2号牙轮", @"3号牙轮", @"4号牙轮", @"5号牙轮", @"6号牙轮", @"7号牙轮", @"8号牙轮", @"9号牙轮", @"10号牙轮", @"11号牙轮", @"12号牙轮", @"13号牙轮", @"14号牙轮", @"15号牙轮"];
    //所有点的集合
    chartView.xValues = @[@10,@12,@14,@15,@11,@12,@13,@15,@11,@10,@14,@12,@11,@12,@13];
    
    chartView.delegate = self;
    //是否允许点击柱子 （点击后变色）
    //chartView.barUserInteractionEnabled = NO;
    [chartView draw];
}

//---------------饼形图-------------
- (void)loadPieData
{
    //图标+文字整体frame
    chartWidth = PhoneScreen_WIDTH/2;
    
    chartHeight = 325/*PhoneScreen_HEIGHT*/;
    
    segmentDataArray = [NSMutableArray arrayWithObjects:@"13",@"10",@"11",@"16", nil];
    
    segmentTitleArray = [NSMutableArray arrayWithObjects:@"一班",@"二班",@"三班",@"四班",nil];
    
    segmentColorArray = [NSMutableArray arrayWithObjects:[UIColor redColor],[UIColor yellowColor],[UIColor greenColor],[UIColor blueColor], nil];
    
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)loadPieChartView
{
    //包含文本的视图frame
    PieChartView = [[CustomPieView alloc]initWithFrame:CGRectMake(chartWidth, 525, chartWidth, chartHeight)];
    
    //数据源数组
    PieChartView.segmentDataArray = segmentDataArray;
    
    //颜色数组，若不传入，则为随即色
    PieChartView.segmentColorArray = segmentColorArray;
    
    //标题，若不传入，则为“其他”
    PieChartView.segmentTitleArray = segmentTitleArray;
    
    //动画时间
    PieChartView.animateTime = 2.0;
    
    //内圆的颜色
    PieChartView.innerColor = [UIColor whiteColor];
    
    //内圆的半径
    PieChartView.innerCircleR = 0;
    
    //大圆的半径
    PieChartView.pieRadius = 130;
    
    //整体饼状图的背景色
    PieChartView.backgroundColor = [UIColor whiteColor];
    //chartView.backgroundColor = RGBCOLOR(240, 241, 242, 1.0);
    
    //圆心位置，此属性会被centerXPosition、centerYPosition覆盖，圆心优先使用centerXPosition、centerYPosition
    PieChartView.centerType = PieCenterTypeCenter;
    
    //是否动画
    PieChartView.needAnimation = YES;
    
    //动画类型，全部只有一个动画；各个部分都有动画
    PieChartView.type = PieAnimationTypeTogether;
    
    //圆心，相对于饼状图的位置（此值-大圆半径 = 大圆距离左边的距离）
    PieChartView.centerXPosition = 50;
    
    //右侧的文本颜色是否等同于模块的颜色
    PieChartView.isSameColor = NO;
    
    //判断设备--决定字体大小
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        //文本的行间距
        PieChartView.textSpace = 20;
        
        //文本的字号
        PieChartView.textFontSize = 13;
        
        //文本的高度
        PieChartView.textHeight = 30;
        
        //文本前的颜色块的高度
        PieChartView.colorHeight = 10;
    }
    else {
        //文本的行间距
        PieChartView.textSpace = 20;
        
        //文本的字号
        PieChartView.textFontSize = 30;
        
        //文本的高度
        PieChartView.textHeight = 30;
        
        //文本前的颜色块的高度
        PieChartView.colorHeight = 10;
    };
    
    
    //文本前的颜色块是否为圆
    PieChartView.isRound = YES;
    
    //文本距离右侧的间距
    PieChartView.textRightSpace = 200;
    
    //支持点击事件
    PieChartView.canClick = YES;
    
    //点击圆饼后的偏移量
    PieChartView.clickOffsetSpace = 20;
    
    //不隐藏右侧的文本
    PieChartView.hideText = NO;
    
    //点击触发的block，index与数据源对应
    [PieChartView clickPieView:^(NSInteger index) {
        NSLog(@"Click Index:%ld",index);
    }];
    
    //添加到视图上
    [PieChartView showCustomViewInSuperView:self.view];
    
}

- (void)loadNewDataArray
{
    NSInteger dataCount = [self getRandomNumber:1 to:7];
    
    NSMutableArray *dataArray = [NSMutableArray array];
    
    for (int i = 0; i< dataCount; i++) {
        
        NSInteger value = [self getRandomNumber:1 to:20];
        
        NSString *valueString = [NSString stringWithFormat:@"%ld",value];
        
        [dataArray addObject:valueString];
        
    }
    
    PieChartView.segmentDataArray = dataArray;
    
}

- (void)loadNewColorArray
{
    NSMutableArray *colorArray = [NSMutableArray array];
    
    for (int i = 0; i< PieChartView.segmentDataArray.count; i++) {
        
        CGFloat red = [self getRandomNumber:1 to:255];
        CGFloat green = [self getRandomNumber:1 to:255];
        CGFloat blue = [self getRandomNumber:1 to:255];
        
        UIColor *color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:1.0];
        [colorArray addObject:color];
        
    }
    
    PieChartView.segmentColorArray = colorArray;
    
    
}

- (int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from)));
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
